/*

 * JDialog.java

 *

 * Created on 18. juli 2003, 17:45

 */



package no.uib.jexpress_modularized.core.visualization.Print;

import javax.swing.*;

import java.awt.print.*;

import java.awt.*;
import java.io.Serializable;
import no.uib.jexpress_modularized.core.visualization.StripedPanel;

//import no.uib.jexpress_modularized.core.visualization.*;









/**

 *

 * @author  bjarte dysvik

 */

public class PrintPreview2 extends javax.swing.JDialog implements Serializable {

    layoutPanel l = new layoutPanel();

     PrinterJob prnJob = PrinterJob.getPrinterJob();

     PageFormat pageformat;

     

     

        private PrinterJob pj;

        private Book book;

        private PageFormat dPF;

        private Component comp;

        private double factor;

        private boolean dlg;

     

     

    /** Creates new form JDialog */

    public PrintPreview2(java.awt.Frame parent, boolean modal) {

        super(parent, modal);

        initComponents();

        if(parent!=null) this.setLocationRelativeTo(parent);

        

        //String name = prnJob.getPrintService().getName();

        pageformat=prnJob.defaultPage();



        jPanel1.add("Center",l);

        printer.setText("DEFAULT");



       pj = PrinterJob.getPrinterJob();

       dPF = pj.defaultPage(); 

    

       

       

       

       //printer.setText(pj.getPrintService().getName());       

        

       

    }

    

    public layoutPanel getLayoutPanel(){

     return l;   

    }

    

    public void setComponent(Component com){

        l.setComponent(com);

        

        

        Dimension dz = com.getSize();

        

        double dx = (pageformat.getImageableWidth()/dz.width);

        double dy = (pageformat.getImageableHeight()/dz.height);

        

        double scale = Math.min(dx,dy);

        

        jSlider1.setValue((int)(scale*100));

        l.setScale(scale,scale);

        l.repaint();

        

    }

    

    

    

    

    /** This method is called from within the constructor to

     * initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is

     * always regenerated by the Form Editor.

     */

    private void initComponents() {//GEN-BEGIN:initComponents

        java.awt.GridBagConstraints gridBagConstraints;



        stripedPanel1 = new StripedPanel();

        jPanel1 = new javax.swing.JPanel();

        jPanel2 = new javax.swing.JPanel();

        jPanel4 = new javax.swing.JPanel();

        jPanel3 = new javax.swing.JPanel();

        jLabel1 = new javax.swing.JLabel();

        jSlider1 = new javax.swing.JSlider();

        jPanel5 = new javax.swing.JPanel();

        jPanel6 = new javax.swing.JPanel();

        jButton1 = new javax.swing.JButton();

        jPanel7 = new javax.swing.JPanel();

        jPanel8 = new javax.swing.JPanel();

        jButton2 = new javax.swing.JButton();

        jLabel2 = new javax.swing.JLabel();

        printer = new javax.swing.JLabel();

        jPanel9 = new javax.swing.JPanel();

        jPanel10 = new javax.swing.JPanel();

        jButton3 = new javax.swing.JButton();

        jPanel11 = new javax.swing.JPanel();

        jLabel3 = new javax.swing.JLabel();



        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent evt) {

                closeDialog(evt);

            }

        });



        stripedPanel1.setLayout(new java.awt.BorderLayout());



        stripedPanel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(4, 4, 4, 4)));

        stripedPanel1.setColor1(new java.awt.Color(204, 204, 204));

        stripedPanel1.setColor2(new java.awt.Color(180, 204, 180));

        jPanel1.setLayout(new java.awt.BorderLayout());



        jPanel1.setBackground(new java.awt.Color(194, 194, 200));

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(100, 100, 120)));

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 600));

        jPanel1.setOpaque(false);

        stripedPanel1.add(jPanel1, java.awt.BorderLayout.CENTER);



        jPanel2.setLayout(new java.awt.GridBagLayout());



        jPanel2.setBackground(new java.awt.Color(194, 194, 200));

        jPanel2.setOpaque(false);

        jPanel4.setLayout(new java.awt.BorderLayout());



        jPanel4.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(100, 100, 120)));

        jPanel3.setLayout(new java.awt.GridBagLayout());



        jPanel3.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(3, 3, 3, 3)));

        jPanel3.setOpaque(false);

        jLabel1.setText("Scale");

        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;

        jPanel3.add(jLabel1, gridBagConstraints);



        jSlider1.setMajorTickSpacing(20);

        jSlider1.setMaximum(200);

        jSlider1.setMinorTickSpacing(5);

        jSlider1.setPaintTicks(true);

        jSlider1.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseReleased(java.awt.event.MouseEvent evt) {

                jSlider1MouseReleased(evt);

            }

        });



        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 0;

        gridBagConstraints.gridy = 1;

        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

        gridBagConstraints.weightx = 10.0;

        jPanel3.add(jSlider1, gridBagConstraints);



        jPanel4.add(jPanel3, java.awt.BorderLayout.CENTER);



        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridwidth = 3;

        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

        gridBagConstraints.weightx = 10.0;

        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);

        jPanel2.add(jPanel4, gridBagConstraints);



        jPanel5.setLayout(new java.awt.BorderLayout());



        jPanel5.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(100, 100, 120)));

        jPanel6.setLayout(new java.awt.GridBagLayout());



        jPanel6.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(3, 3, 3, 3)));

        jPanel6.setOpaque(false);

        jButton1.setText("Print");

        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton1ActionPerformed(evt);

            }

        });



        jPanel6.add(jButton1, new java.awt.GridBagConstraints());



        jPanel5.add(jPanel6, java.awt.BorderLayout.CENTER);



        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 2;

        gridBagConstraints.gridy = 1;

        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);

        jPanel2.add(jPanel5, gridBagConstraints);



        jPanel7.setLayout(new java.awt.BorderLayout());



        jPanel7.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(100, 100, 120)));

        jPanel8.setLayout(new java.awt.GridBagLayout());



        jPanel8.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(3, 3, 3, 3)));

        jPanel8.setOpaque(false);

        jButton2.setText("Change");

        jButton2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton2ActionPerformed(evt);

            }

        });



        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 2;

        gridBagConstraints.gridy = 0;

        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;

        jPanel8.add(jButton2, gridBagConstraints);



        jLabel2.setText("Printer:");

        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 0;

        gridBagConstraints.gridy = 0;

        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;

        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);

        jPanel8.add(jLabel2, gridBagConstraints);



        printer.setForeground(new java.awt.Color(0, 0, 150));

        printer.setText("NONE");

        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 1;

        gridBagConstraints.gridy = 0;

        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;

        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;

        gridBagConstraints.weightx = 10.0;

        gridBagConstraints.insets = new java.awt.Insets(0, 7, 0, 7);

        jPanel8.add(printer, gridBagConstraints);



        jPanel7.add(jPanel8, java.awt.BorderLayout.CENTER);



        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 0;

        gridBagConstraints.gridy = 1;

        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

        gridBagConstraints.weightx = 8.0;

        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);

        jPanel2.add(jPanel7, gridBagConstraints);



        jPanel9.setLayout(new java.awt.BorderLayout());



        jPanel9.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(100, 100, 120)));

        jPanel10.setLayout(new java.awt.GridBagLayout());



        jPanel10.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(3, 3, 3, 3)));

        jPanel10.setOpaque(false);

        jButton3.setText("Cancel");

        jButton3.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton3ActionPerformed(evt);

            }

        });



        jPanel10.add(jButton3, new java.awt.GridBagConstraints());



        jPanel9.add(jPanel10, java.awt.BorderLayout.CENTER);



        gridBagConstraints = new java.awt.GridBagConstraints();

        gridBagConstraints.gridx = 1;

        gridBagConstraints.gridy = 1;

        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);

        jPanel2.add(jPanel9, gridBagConstraints);



        stripedPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);



        jPanel11.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));



        jPanel11.add(jLabel3);



        stripedPanel1.add(jPanel11, java.awt.BorderLayout.NORTH);



        getContentPane().add(stripedPanel1, java.awt.BorderLayout.CENTER);



        pack();

    }//GEN-END:initComponents



    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        setVisible(false);

        dispose();

    }//GEN-LAST:event_jButton3ActionPerformed



    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

       

            //dPF = pj..pageDialog(dPF);

           // pj.setPrintable(new CompPrint(l.cpn.com), dPF);

             

             //l.setPageFormat(dPF);  

             

             pj.printDialog();

             

             

             printer.setText(pj.getPrintService().getName());

        

    }//GEN-LAST:event_jButton2ActionPerformed



    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

     

        no.uib.jexpress_modularized.core.visualization.SwingWorker  worker = new no.uib.jexpress_modularized.core.visualization.SwingWorker() {

                public Object construct() {

                    

        try{

            javax.print.PrintService printService = pj.getPrintService();

        

            

            l.cpn.scaleX=l.scaleX;

            l.cpn.scaleY=l.scaleY;

            

      

         

       if(printService!=null) pj.setPrintService( printService );

       pj.setPrintable(l.cpn, dPF);

       

       jLabel3.setText("Printing");

       

       pj.print();

       

       

       

     }

     catch(Exception e){

     

         javax.swing.JOptionPane.showMessageDialog(PrintPreview2.this,"Could not print component\nError:\n"+e.toString(), "Print Error",javax.swing.JOptionPane.ERROR_MESSAGE);

         

         e.printStackTrace();

     

     }

              return null;  }

                

                public void finished(){

                    

     jLabel3.setText("");

     dispose();

     

                }

        };

        worker.start();

     

     

    }//GEN-LAST:event_jButton1ActionPerformed



    private void jSlider1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseReleased

        l.setScale((double)jSlider1.getValue()/100.0, (double)jSlider1.getValue()/100.0);

        l.repaint();

    }//GEN-LAST:event_jSlider1MouseReleased

    

    /** Closes the dialog */

    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog

        setVisible(false);

        dispose();

    }//GEN-LAST:event_closeDialog

    

    /**

     * @param args the command line arguments

     */

    public static void main(String args[]) {

        new PrintPreview2(new javax.swing.JFrame(), true).setVisible(true);

    }

    

    

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JButton jButton1;

    private javax.swing.JButton jButton2;

    private javax.swing.JButton jButton3;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JLabel jLabel2;

    private javax.swing.JLabel jLabel3;

    private javax.swing.JPanel jPanel1;

    private javax.swing.JPanel jPanel10;

    private javax.swing.JPanel jPanel11;

    private javax.swing.JPanel jPanel2;

    private javax.swing.JPanel jPanel3;

    private javax.swing.JPanel jPanel4;

    private javax.swing.JPanel jPanel5;

    private javax.swing.JPanel jPanel6;

    private javax.swing.JPanel jPanel7;

    private javax.swing.JPanel jPanel8;

    private javax.swing.JPanel jPanel9;

    private javax.swing.JSlider jSlider1;

    private javax.swing.JLabel printer;

    private StripedPanel stripedPanel1;

    // End of variables declaration//GEN-END:variables

    

}
class layoutPanel extends JPanel{

    int m_wPage =0;

    int m_hPage = 0;

    //componentPane cpn=new componentPane();

    CompPrint cpn = new CompPrint();

    

    double scaleX = 0.5;

    double scaleY = 0.5;

    

   // Rectangle paper = new Rectangle();

    PageFormat pageformat = PrinterJob.getPrinterJob().defaultPage();

    

    

    public void setScale(double x,double y){

        scaleX=x;

        scaleY=y;

        //cpn.setScale(x,y);

        cpn.scaleX=scaleX;

        cpn.scaleY=scaleY;

    }

    

    

 public void setPageFormat(PageFormat pageformat){

     this.pageformat=pageformat;

     //PageFormat pageFormat = prnJob.getPrinterJob().defaultPage();

    // paper=new Rectangle((int)pageformat.getImageableX(),(int)pageformat.getImageableY(),(int)pageformat.getImageableWidth(),(int)pageformat.getImageableHeight());

     repaint();                        

 }

    

    

    

 public layoutPanel(){

 /*

     JLabel l = new JLabel("This is a test");

     l.setPreferredSize(new Dimension(200,500));

     l.setBorder(BorderFactory.createLineBorder(Color.red));

     

     JFrame f = new JFrame();

     f.getContentPane().add(l);

     f.pack();

     cpn.setComponent(l);

     f.setVisible(true);

   */

  setPreferredSize(new Dimension(400,500));   

      

 }

    

    

  public void setComponent(Component com){

        cpn.setComponent(com);

    }

 

 

 

    public void paintComponent(Graphics g){

        

            Graphics2D g2d = (Graphics2D)g;

            

             Rectangle paper=new Rectangle((int)pageformat.getImageableX(),(int)pageformat.getImageableY(),(int)pageformat.getImageableWidth(),(int)pageformat.getImageableHeight());

          

            int W = this.getWidth();

            int H = this.getHeight();

           

            double scalew = (double)W/(pageformat.getWidth());

            double scaleh = (double)H/(pageformat.getHeight());

            

            double scale = Math.min(scalew,scaleh);

            

            g.setColor(Color.gray);

            g.fillRect(0,0,getWidth(), getHeight());

            

            g.setColor(Color.white);

            

            g2d.scale(scale,scale);

            

            g.fillRect(10,10,(int)(pageformat.getWidth()-20), (int)(pageformat.getHeight()-20));

            

            g.setColor(new Color(130,130,200));

 

            g.drawLine(paper.x,paper.y,paper.x+20,paper.y);

            g.drawLine(paper.x,paper.y,paper.x,paper.y+20);

            

            g.drawLine(paper.width+paper.x-20,paper.y,paper.width+paper.x,paper.y);

            g.drawLine(paper.width+paper.x,paper.y,paper.width+paper.x,paper.y+20);

            

            g.drawLine(paper.width+paper.x-20,paper.y+paper.height,paper.width+paper.x,paper.y+paper.height);

            g.drawLine(paper.width+paper.x,paper.y-20+paper.height,paper.width+paper.x,paper.y+paper.height);

            

            g.drawLine(paper.x,paper.y+paper.height,paper.x+20,paper.y+paper.height);

            g.drawLine(paper.x,paper.y-20+paper.height,paper.x,paper.y+paper.height);

            

            Dimension sz = cpn.getComponent().getSize();

            Rectangle r = new Rectangle(0,0,sz.width,sz.height);

          

            int CenterX = (int)paper.getCenterX();

            int CenterY = (int)paper.getCenterY();

            

            int comCenterY = (int)r.getCenterY();

            int comCenterX = (int)r.getCenterX();

            

            double CX = (double) CenterX/((scaleX));

            double CY = (double) CenterY/((scaleY));

            

            double cCY = (double) comCenterY;//*scale*scaleY;

            double cCX = (double) comCenterX;//*scale*scaleX;

            

            g2d.scale(scaleX,scaleY);

            

            g2d.translate((int)(CX - cCX),(int)(CY - cCY));



            //

            

            Rectangle r2 = new Rectangle(0,0,sz.width,sz.height);

  

            cpn.paint(g);

            

            g2d.setColor(Color.black);

            g2d.draw(r2);

            

            

            g2d.translate(-(int)(CX - cCX),-(int)(CY - cCY));

            g2d.scale(1.0/scaleX,1.0/scaleY);

            g2d.scale(1.0/scale,1.0/scale);

           

          

            

            

            //g2d.scale(1/(scaleX*scale),1/(scaleY*scale));

            

            //cpn.com.paint(g);

            

           // g2d.scale(1.0/scalew,1.0/scaleh);

        //    g2d.scale(1.0/0.2,1.0/0.2);

    }

    

    

    /*

    public void Print(PrinterJob prnJob){

        

        ForumPrint p = new ForumPrint(cpn.com, 72,true);

        p.print();

    }

      */

}



/*



class componentPane implements Printable{

 Component com;

 double scaleX = 0.5;

    double scaleY = 0.5;

    

    

    public void setScale(double x,double y){

        scaleX=x;

        scaleY=y;

        

    }

 

 public componentPane(){

    }

 

 public componentPane(Component com){

        this.com = com;

    }

    

  

 public void setComponent(Component com){

        this.com = com;

    }



    

 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

            

     if(pageIndex>0) return this.NO_SUCH_PAGE;

     

     graphics.translate((int)pageFormat.getImageableX(),(int) pageFormat.getImageableY());

     

        Graphics2D g2d = (Graphics2D)graphics;

        g2d.scale(scaleX,scaleY);

        com.printAll(graphics);

        return this.PAGE_EXISTS;

 } 

    

}



*/



class CompPrint implements Printable {

    public double scaleX,scaleY;

    public int CenterX,CenterY;

    private Component comp;

    

    public CompPrint(Component comp){

    this.comp=comp;

    }

    

    public Component getComponent(){

     return comp;   

    }

    

    

     public CompPrint(){

    this.comp=comp;

    }

    

     public void setComponent(Component cpn){

         this.comp=cpn;

     }

     

    

    public void paint(Graphics g){

     comp.paint(g);  

    }

    

    public Rectangle getBounds(){

     return comp.getBounds();   

    }

    

    

public int print(Graphics g, PageFormat pf, int page) {

Graphics2D g2d = (Graphics2D) g; 

g2d.setPaint(Color.black);



    

        Rectangle paper = new Rectangle((int)pf.getImageableX(),(int)pf.getImageableY(),(int)pf.getImageableWidth(),(int)pf.getImageableHeight());

        //Rectangle r = comp.getBounds();

        Dimension sz = comp.getSize();    

        Rectangle r = new Rectangle(0,0,sz.width,sz.height);

     

                    

        g2d.scale(scaleX,scaleY);

        

            int CenterX = (int)paper.getCenterX();

            int CenterY = (int)paper.getCenterY();

                      

            int comCenterY = (int)r.getCenterY();

            int comCenterX = (int)r.getCenterX();

         

            double CX = (double) CenterX/((scaleX));

            double CY = (double) CenterY/((scaleY));

            

            double cCY = (double) comCenterY;//*scale*scaleY;

            double cCX = (double) comCenterX;//*scale*scaleX;

            



          //  g.drawLine(CenterX, 0, CenterX,100);

            

            

            

            g.translate((int)(CX - cCX),(int)(CY - cCY));

            

            

           // g.translate((int)(CenterX - cCX),(int)(CenterY - cCY));

            

           // g2d.scale(scaleX,scaleY);

            

            //cpn.com.paint(g);



//g2d.translate(pf.getImageableX(), pf.getImageableY());

//g2d.scale( 1 / factor, 1 / factor);



            comp.paint(g2d); 

            

            

if (page >= 1) {

return Printable.NO_SUCH_PAGE;

} else {

return Printable.PAGE_EXISTS;

}

} 

}











